﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;

public partial class Evaluate_TaskEvaluates : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

       
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320101"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        } 
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (departmentid.Substring(0, 4) != "1300")
        {
            Response.Redirect("TaskEvaluatesDetachment.aspx");
        }
        if (!IsPostBack)
        {
            BindYearList();
            BindMonthList();
            CreateTable();
        }
    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();

        if (Request["Year"] != null)
        {
            YearList.SelectedValue = Request["Year"];            
        }
       
    }
    private void BindMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            MonthList.Items.Add(li);
        }
        MonthList.SelectedValue = DateTime.Now.Month.ToString();
        if (Request["Month"] != null)
        {
            MonthList.SelectedValue = Request["Month"];
            
        }
    }
    private void CreateTable()
    {
        DataSet ds = SplitPage.ExecuteSQL("SELECT AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,1) = '0' AND SUBSTRING(DepartmentId,6,1) = '0' AND SUBSTRING(DepartmentId,7,1) = '0' AND Left(DepartmentID,4)<>'1314' AND Left(DepartmentID,4)<>'1315' AND Left(DepartmentID,4)<>'1313'");
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL varchar(8000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN UserId = ''' + DepartmentId + ''' THEN Score END) AS D' + DepartmentId FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,1) = '0' AND SUBSTRING(DepartmentId,6,1) = '"+Session["DepartmentId"].ToString().Substring(5,1)+"' AND SUBSTRING(DepartmentId,7,1) = '0' AND Left(DepartmentID,4)<>'1314' AND Left(DepartmentID,4)<>'1315' AND Left(DepartmentID,4)<>'1313'");
        sql.AppendLine("SET @SQL = 'SELECT TaskId ' +@SQL + ' FROM TaskUsers WHERE TaskId IN (SELECT TaskId FROM Tasks WHERE YEAR(EndDate) =" + YearList.SelectedValue + " AND MONTH(EndDate)=" + MonthList.SelectedValue + " AND CreateDepartment=''" + Session["DepartmentID"].ToString() + "'') GROUP BY TaskId'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;

        Table tb = new Table();
        tb.Caption = "<span>" + YearList.SelectedItem.Text + MonthList.SelectedItem.Text + "工作任务审核</span>";
        tb.Width = Unit.Percentage(100);
        tb.CellPadding = 4;
        tb.CellSpacing = 0;
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tr.CssClass = "grid_header";
        tc.Text = "";
        tc.Width = Unit.Pixel(30);
        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Text = "任务名称";
        tr.Cells.Add(tc);

        int col = ds.Tables[0].Rows.Count;

        for (int i = 0; i < col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Width = Unit.Pixel(40);
            tc.Style.Add("writing-mode", "tb-lr");
            tc.Style.Add("white-space", "normal");
            tc.Text = ds.Tables[0].Rows[i][0].ToString();
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        int row = 1;
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = row.ToString();
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.BackColor = System.Drawing.Color.White;

            string title = Task.GetTask((int)drv[0]).Title.Length <= 30 ? Task.GetTask((int)drv[0]).Title : Task.GetTask((int)drv[0]).Title.Substring(0, 30);

            tc.Text = title;
	    tc.ToolTip=title;
            tr.Cells.Add(tc);
            for (int i = 1; i <= col; i++)
            {
                tc = new TableCell();
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                string score = "";
                if ((decimal)drv[i] > 0)
                    score = ((decimal)drv[i]).ToString("0");
                else
                    score = "0";
                tc.Text = "<a href=TaskEvaluate.aspx?RootId=" + drv[0].ToString() + "&UserId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + score + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }
        PlaceHolder.Controls.Add(tb);
    }
    protected void ButtonEvaluate_Click(object sender, EventArgs e)
    {
        CreateTable();
    }
}
